/*******************************************************************************
 * BSD 3-Clause License
 *
 * Copyright (c) 2021, Qiayuan Liao
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 *
 * * Neither the name of the copyright holder nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/

//
// Created by qiayuan on 12/27/20.
//

/********************************************************************************
Modified Copyright (c) 2024-2025, Dm Robotics. 
********************************************************************************/

#include "dm_hw/DmHWLoop.h"
#include "dm_hw/DmHW.h"
#include "std_msgs/Bool.h"
#include "kezi_msgs/MotorControlCmd.h"
std::shared_ptr<damiao::DmHW> motor_dm_hw;
damiao::Control_Mode cur_mode;

void MoterEnableControl(const std_msgs::Bool::ConstPtr &msg){
  if(msg->data) {
    motor_dm_hw->MoterEnable();
  } else {
    motor_dm_hw->MoterDisable();
  }

}
void MoterControlCmdCallback(const kezi_msgs::MotorControlCmd::ConstPtr &msg){
  switch (msg->mode)
  {
    case 0:
      
      break;
    case 1:
      break;
    case 2:
      if (cur_mode != damiao::VEL_MODE)
      {
        motor_dm_hw->MoterModeSetting(damiao::VEL_MODE);
        cur_mode = damiao::VEL_MODE;
        // std::cout << "damiao::VEL_MODE" << std::endl;
      }
      motor_dm_hw->MoterVelControl(msg->id, msg->velocity);
      // std::cout << "id: " << msg->id << "     "  <<  "vel: " << msg->velocity << std::endl;
      break;
    
    default:
      break;
  }

}



int main(int argc, char** argv)
{
  ros::init(argc, argv, "damiao");
  ros::NodeHandle nh;
  ros::NodeHandle robot_hw_nh("damiao");
  ros::Subscriber sub = nh.subscribe<std_msgs::Bool>("kezi/moter_is_enable",10,MoterEnableControl);
  auto moter_sub = nh.subscribe<kezi_msgs::MotorControlCmd>("kezi/moter_control",10,MoterControlCmdCallback);
  

  // Run the hardware interface node
  // -------------------------------

  // We run the ROS loop in a separate thread as external calls, such
  // as service callbacks loading controllers, can block the (main) control loop

  ros::AsyncSpinner spinner(3);
  spinner.start();

  try
  {
    motor_dm_hw = std::make_shared<damiao::DmHW>();

    motor_dm_hw->init(robot_hw_nh, robot_hw_nh);

    damiao::DmHWLoop control_loop(nh, motor_dm_hw);

    // ros::waitForShutdown();

    // motor_dm_hw->MoterModeSetting(damiao::VEL_MODE);
    // motor_dm_hw->MoterVelControl();
    while (ros::ok)
    {
      /* code */
      ros::spinOnce();
    }
    
  }
  catch (const ros::Exception& e)
  {
    ROS_FATAL_STREAM("Error in the hardware interface:\n"
                     << "\t" << e.what());
    return 1;
  }

  return 0;
}
